package gui;
	
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Observer;

import javax.swing.*;
import javax.swing.event.*;
import domein.models.*;
import domein.*;

public class DownloadManager extends JPanel implements Observer 
{
	private static final long serialVersionUID = 1L;
	private DownloadsTableModel tableModel;
    private JTable downloadTable;     
    private JButton pauseButton, resumeButton, cancelButton, clearButton;
   
    public DownloadManager() 
    {
        setName("Download Manager");        
        setSize(640, 480);
              

        tableModel = PeerController.getInstance().getDownloadsTableModel();
        downloadTable = new JTable(tableModel);
        
        downloadTable.getSelectionModel().addListSelectionListener(new
                ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                tableSelectionChanged();
            }
        });
        
        downloadTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        JPanel downloadsPanel = new JPanel();
        downloadsPanel.setBorder(BorderFactory.createTitledBorder("Downloads"));
        downloadsPanel.setLayout(new BorderLayout());
        downloadsPanel.add(new JScrollPane(downloadTable), BorderLayout.CENTER);
        
        JPanel buttonsPanel = new JPanel();
        pauseButton = new JButton("Pause");
        pauseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //actionPause();
            }
        });
        
        pauseButton.setEnabled(false);
        buttonsPanel.add(pauseButton);
        resumeButton = new JButton("Resume");
        resumeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //actionResume();
            }
        });
        
        resumeButton.setEnabled(false);
        buttonsPanel.add(resumeButton);
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //actionCancel();
            }
        });
        
        cancelButton.setEnabled(false);
        buttonsPanel.add(cancelButton);
        clearButton = new JButton("Clear");
        clearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //actionClear();
            }
        });
        
        clearButton.setEnabled(false);
        buttonsPanel.add(clearButton);

        this.setLayout(new BorderLayout());
        this.add(downloadsPanel, BorderLayout.CENTER);
        this.add(buttonsPanel, BorderLayout.SOUTH);
    }

    private void tableSelectionChanged() 
    {
        //if (selectedDownload != null)
        //   selectedDownload.deleteObserver(DownloadManager.this);
          
    	//if (!clearing) {
    	//     selectedDownload =
        //            tableModel.getDownload(table.getSelectedRow());
        //   selectedDownload.addObserver(DownloadManager.this);
        //updateButtons();
    	//}
    }
    
    /*private void actionPause() 
    {
        selectedDownload.pause();
        updateButtons();
    }
    
    private void actionResume() 
    {
       selectedDownload.resume();
        updateButtons();
    }
    
    private void actionCancel() 
    {
        selectedDownload.cancel();
        updateButtons();
    }
    
    private void actionClear() 
    {
        clearing = true;
        //tableModel.clearDownloadList(table.getSelectedRow());
        clearing = false;
        selectedDownload = null;
        updateButtons();
    }
    
    private void updateButtons() 
    {
        if (selectedDownload != null) {
            int status = selectedDownload.getStatus();
            switch (status) {
                case DownloadFile.DOWNLOADING:
                    pauseButton.setEnabled(true);
                    resumeButton.setEnabled(false);
                    cancelButton.setEnabled(true);
                    clearButton.setEnabled(false);
                    break;
                case DownloadFile.PAUSED:
                    pauseButton.setEnabled(false);
                    resumeButton.setEnabled(true);
                    cancelButton.setEnabled(true);
                    clearButton.setEnabled(false);
                    break;
                case DownloadFile.ERROR:
                    pauseButton.setEnabled(false);
                    resumeButton.setEnabled(true);
                    cancelButton.setEnabled(false);
                    clearButton.setEnabled(true);
                    break;
                default: 
                    pauseButton.setEnabled(false);
                    resumeButton.setEnabled(false);
                    cancelButton.setEnabled(false);
                    clearButton.setEnabled(true);
            }
        } else {
            pauseButton.setEnabled(false);
            resumeButton.setEnabled(false);
            cancelButton.setEnabled(false);
            clearButton.setEnabled(false);
        }
    }
   
 	public void addDownload()
 	{
   		tableModel.addDownload(new Download(peerFile));
    }*/
    
    public void update(Observable o, Object arg) {
        //if (selectedDownload != null && selectedDownload.equals(o))
            //updateButtons();
    }
}
